package com.gljx.web.service.impl;

import com.gljx.util.GetFid;
import com.gljx.web.Entity.RefuelCardReg;
import com.gljx.web.Entity.RefuelCardRegEntry;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.RefuelCardRegEntryMapper;
import com.gljx.web.dao.RefuelCardRegMapper;
import com.gljx.web.service.RefuelCardRegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RefuelCardRegServiceImpl extends CommonDao implements RefuelCardRegService {

	@Autowired
	private RefuelCardRegMapper refuelCardRegMapper;
	
	@Autowired
	private RefuelCardRegEntryMapper refuelCardRegEntryMapper;
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.RefuelCardRegMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.RefuelCardRegMapper.pagemap", paramMap, null);
        return paramMap;
	}

	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return refuelCardRegMapper.selectFnumber(companyId);
	}

	@Override
	public int addRefuelCardReg(RefuelCardReg refuelCardReg, String[] fseq, String[] carId, String[] cfqty, String[] cfprice, String[] cfamount, String[] cfdesc, String[] cftrainid, String[] cfkemu, String[] cfdepname, String[] cfgroupid,String [] cfteamid, String[] cfoilspecid, String[] cfcartype,String[]cfbizdate) {
		// TODO Auto-generated method stub
		int row1=0;
		int row2=0;
		//添加父类表
		row1=refuelCardRegMapper.insertSelective(refuelCardReg);
		//添加子类
		for (int i = 0; i < fseq.length; i++) {
			RefuelCardRegEntry refuelCardRegEntry = new RefuelCardRegEntry();
			refuelCardRegEntry.setFid(GetFid.UUIDIntoFid());
			refuelCardRegEntry.setFseq(Long.valueOf(fseq[i]));
			refuelCardRegEntry.setFparentid(refuelCardReg.getFid());
			refuelCardRegEntry.setCfcarid(carId[i]);
			refuelCardRegEntry.setCfqty(new BigDecimal(cfqty[i]));
			refuelCardRegEntry.setCfprice(new BigDecimal(cfprice[i]));
			refuelCardRegEntry.setCfamount(new BigDecimal(cfamount[i]));
			refuelCardRegEntry.setCfdesc(cfdesc[i]);

			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
			refuelCardRegEntry.setCftrainid(cftrainid[i]);//教练id
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfkemu(Long.parseLong(cfkemu[i]));//科目
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfcartype(cfcartype[i]);//车型
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfoilspecid(cfoilspecid[i]);//汽油的规格
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfdepname(cfdepname[i]);//所属的部门名称
			}
			if(cfteamid[i]!=null||"".equals(cfteamid[i])){
				refuelCardRegEntry.setCfteamcoach(cfteamid[i]);//所属的教练队名称
			}
			if(cfbizdate[i]!=null||"".equals(cfbizdate[i])){
				refuelCardRegEntry.setCfbizdate(cfbizdate[i]);//所属的车辆的加油时间
			}

			row2+=refuelCardRegEntryMapper.insertSelective(refuelCardRegEntry);
		}
		if(row1 >0 && row2>0){
			return 1;
		}
		return 0;
	}


	@Override
	public int deleteRefuelCardRegByFid(String fid) {
		// TODO Auto-generated method stub
		return refuelCardRegMapper.deleteByPrimaryKey(fid);
	}

	@Override
	public RefuelCardReg selRefuelCardRegByFid(String fid) {
		// TODO Auto-generated method stub
		return refuelCardRegMapper.selectByPrimaryKey(fid);
	}

	@Override
	public int updateRefuelCardReg(RefuelCardReg refuelCardReg,String [] fseq,String [] carId,String [] cfqty,String [] cfprice,String [] cfamount,String [] cfdesc,
								   String []cftrainid,String []cfkemu,String []cfdepname,String[]cfgroupid,String[] cfteamid,String []cfoilspecid,String[]cfcartype,String[] cfbizdate) {
		// TODO Auto-generated method stub
		//修改
		refuelCardRegMapper.updateByPrimaryKeySelective(refuelCardReg);
		//根据父类id删除子类
		int row1 = refuelCardRegEntryMapper.deleteByParentId(refuelCardReg.getFid());
		//添加子类
		int row2=0;
		for (int i = 0; i < fseq.length; i++) {
			RefuelCardRegEntry refuelCardRegEntry = new RefuelCardRegEntry();
			refuelCardRegEntry.setFid(GetFid.UUIDIntoFid());
			refuelCardRegEntry.setFseq(Long.valueOf(fseq[i]));
			refuelCardRegEntry.setFparentid(refuelCardReg.getFid());
			refuelCardRegEntry.setCfcarid(carId[i]);
			refuelCardRegEntry.setCfqty(new BigDecimal(cfqty[i]));
			refuelCardRegEntry.setCfprice(new BigDecimal(cfprice[i]));
			refuelCardRegEntry.setCfamount(new BigDecimal(cfamount[i]));
			refuelCardRegEntry.setCfdesc(cfdesc[i]);
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCftrainid(cftrainid[i]);//教练id
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfkemu(Long.parseLong(cfkemu[i]));//科目
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfcartype(cfcartype[i]);//车型
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfoilspecid(cfoilspecid[i]);//汽油的规格
			}
			if(cftrainid[i]!=null||"".equals(cftrainid[i])){
				refuelCardRegEntry.setCfdepname(cfdepname[i]);//所属的部门名称
			}
			if(cfteamid[i]!=null||"".equals(cfteamid[i])){
				refuelCardRegEntry.setCfteamcoach(cfteamid[i]);//所属的教练队名称
			}
			if(cfbizdate[i]!=null||"".equals(cfbizdate[i])){
				refuelCardRegEntry.setCfbizdate(cfbizdate[i]);//加油时间
			}

			row2+=refuelCardRegEntryMapper.insertSelective(refuelCardRegEntry);
		}
		if( row1 > 0 && row2 >0){
			return 1;
		}
		return 0;
	}

	@Override
	public int updateRefuelCardReg(RefuelCardReg refuelCardReg) {
		// TODO Auto-generated method stub
		return refuelCardRegMapper.updateByPrimaryKeySelective(refuelCardReg);
	}

	@Override
	public List<RefuelCardReg> selectRefuelCardReg(String companyId, String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return refuelCardRegMapper.selectRefuelCardReg(map);
	}

}
